### 
#Usage: Rscript pancancer_plot.R -jt FGFR2_LS002.csv -log TRUE
#       Rscript pancancer_plot.R -jt FGFR2_LS002.csv -log FALSE    
###

library(getopt)
library(ggplot2)
library(ggpubr)
library(dplyr)
spec <- matrix(
  c("junction_table",  "jt", 2, "character", "This is cpm table!",
    "log_scale", "log", 2, "character",  "whether log!"),
  byrow=TRUE, ncol=5)
args=getopt(spec)
data=read.csv(args$junction_table,header=F)
colnames(data)=c("cpm","class","cancer")
data$class = gsub('cancer', 'T', data$class)
data$class = gsub('NT', 'N', data$class)
data$cancer=as.factor(data$cancer)
data$class=as.factor(data$class)
data2=data %>% tbl_df() %>% group_by(cancer,class) %>% mutate(snum =max(row_number()))
data2$sample=paste(data2$class,"(",data2$snum,")",sep="")
data2=as.data.frame(data2)
#不能按order(data$class)排序，图中NT和cancer的位置顺序需一致
data3=data2[order(data$class, decreasing = TRUE),]
#levels(data3$class)=c("cancer","NT")
levels(data3$class)=c("N","T")
#原始
if (args$log=="TRUE"){
  data3$cpm=log2(data3$cpm+1)
  #width=(600*5)/(72*4)=10.41667英寸 height=(600*3)/(72*4)=6.25英寸
  #png(file="pancancer_plot_log.png",width=600*5,height=600*3,res=72*4)
  pdf(file="pancancer_plot_log.pdf", width=10.5, height=6.5)
  p = ggboxplot(data3, x="sample",y="cpm",color="class", palette =c("#00AFBB", "#F8766D"),add = "jitter",add.params =list(size=0.5),outlier.shape= NA)+
    facet_wrap( ~cancer,nrow=1,scales ="free_x")+
    xlab("")+
    ylab("log2(CPM+1)")+
    scale_y_continuous(expand = c(0, 0))+
    theme(axis.title.y = element_text(size = 10, face = "bold"))+
    theme(axis.text.x = element_text(size =8,angle = 60, hjust = 1, face="bold"))+
    theme(axis.text.y = element_text(size =8,angle = 360, hjust = 1, face="bold"))+
    theme(strip.text=element_text(face="bold",size=rel(0.6),angle =45),#分面块字体大小和角度
          strip.background = element_rect(color = "white", fill = "white"),#分面块边框和背景颜色
          panel.spacing=unit(0.1,"cm"))+
    theme(panel.background = element_rect(colour = "gray",size =1))+
    theme(legend.position="top", legend.text = element_text(size =10, face="bold"))
  print(p)
  dev.off()
}else{
  pdf(file="pancancer_plot_noLog.pdf", width=10.5, height=6.5)
  p = ggboxplot(data3, x="sample",y="cpm",color="class", palette =c("#00AFBB", "#F8766D"), add = "jitter",add.params =list(size=0.5),outlier.shape= NA)+
    facet_wrap( ~cancer,nrow=1,scales ="free_x")+
    xlab("")+
    ylab("CPM")+
    scale_y_continuous(expand = c(0, 0))+
    theme(axis.title.y = element_text(size = 10, face = "bold"))+
    theme(axis.text.x = element_text(size =8,angle = 60, hjust = 1, face="bold"))+
    theme(axis.text.y = element_text(size =8,angle = 360, hjust = 1, face="bold"))+
    theme(strip.text=element_text(face="bold",size=rel(0.6),angle =45),#分面块字体大小和角度
          strip.background = element_rect(color = "white", fill = "white"),#分面块边框和背景颜色
          panel.spacing=unit(0.1,"cm"))+
    theme(panel.background = element_rect(colour = "gray",size =1))+
    theme(legend.position="top", legend.text = element_text(size =10, face="bold"))
  print(p)
  dev.off()
}
